package com.corpgovernment.job.controller.vo.job;

import com.corpgovernment.api.travelstandard.vo.JobVo;
import com.corpgovernment.job.dao.entity.db.JobDo;
import com.corpgovernment.job.dao.entity.db.JobTypeDo;
import com.corpgovernment.organization.entity.db.MbOrgInfo;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ldma
 * @date 2024/1/10
 */
@Data
public class PageJobRespVo {

    private Integer jobCount;
    private List<Job> jobList;

    @Data
    public static class Job {
        private Long jobId;
        private String jobName;
        private String jobCode;
        private List<JobOrg> jobOrgList;
        private Integer jobOrgCount;
        private Long jobTypeId;
        private String jobTypeName;
        private Integer employeeCount;

        public Job(JobDo jobDo, Map<Long, JobTypeDo> jobTypeMap, Map<Long, Long> employeeCountMap, Map<String, MbOrgInfo> orgInfoMap, Map<Long, List<String>> jobOrgRelationMap) {
            jobId = jobDo.getId();
            jobName = jobDo.getName();
            jobCode = jobDo.getCode();
            List<String> jobOrgIdList = jobOrgRelationMap.get(jobId);
            if (CollectionUtils.isNotEmpty(jobOrgIdList)) {
                jobOrgList = jobOrgIdList.stream().map(item -> {
                    // 岗位所属组织软关联
                    MbOrgInfo mbOrgInfo = orgInfoMap.get(item);
                    if (mbOrgInfo == null) {
                        return null;
                    }
                    return new JobOrg(item, mbOrgInfo.getName());
                }).filter(Objects::nonNull).collect(Collectors.toList());
                jobOrgCount = jobOrgList.size();
            }
            jobTypeId = jobDo.getJobTypeId();
            jobTypeName = jobTypeMap.getOrDefault(jobTypeId, new JobTypeDo()).getName();
            employeeCount = employeeCountMap.getOrDefault(jobId, 0L).intValue();
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class JobOrg {
        private String jobOrgId;
        private String jobOrgName;
    }

}
